<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_CommentController extends Zend_Rest_Controller
{

	public function init()
	{
		$this->_helper->viewRenderer->setNoRender(true);
	}


	public function deleteAction() {

	}

	public function getAction() {

	}

	public function headAction() {

	}

	public function indexAction() {
		try{
			$comment_mapper = new Api_Model_CommentMapper();
			$photo_id = $this->_getParam('param1');
			$comment_limit = $this->_getParam('batch');
			if ($photo_id != NULL){

				$comment = $comment_mapper->getAllComment($photo_id, $comment_limit);
				$this->getresponse()->appendBody(json_encode($comment));
				$this->getResponse()->setHttpResponseCode(200);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function postAction() {
		try{
			$comment_mapper = new Api_Model_CommentMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);
			if ($data['photo_id'] != "" || $data['user_id'] != "" || $data['content'] != ""
			|| $data['location_id']){

				$comment = $comment_mapper->sendComment($data);
				$this->getresponse()->appendBody(json_encode($comment));
				$this->getResponse()->setHttpResponseCode(201);
				//throw new Exception(json_encode(array('meta' => 'Created')),201);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function putAction() {

	}
}

